//
//  RemoteController.m
//  Podcopter
//
//  Created by Aaron Thompson on 2/22/10.
//  Copyright 2010 Vanderbilt University. All rights reserved.
//

#import "RemoteController.h"
#import "AccelerationConversions.h"

@implementation RemoteController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil connection:(GamePeer *)conn
{
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	if (self != nil) {
		activeConnection = conn;
        [activeConnection setDelegate: self];
	}

	return self;
}

- (void)viewDidLoad {
	[[UIAccelerometer sharedAccelerometer] setDelegate:self];
	[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/60.0];
}

- (void)viewWillAppear:(BOOL)animated {
	[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
	self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
//	[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
	return orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft;
}

- (void)dealloc {
	[super dealloc];
}


#pragma mark MomentarySliderViewDelegate

- (void)momentarySlider:(MomentarySliderView *)slider valueChanged:(float)newValue
{
	// Well, this ended up being pointless since we're getting
	//  the fresh slider value with every new acceleration
}

#pragma mark UIAccelerometerDelegate Functionality

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)a
{
    [pitchLabel setText: [NSString stringWithFormat:@"%.3fº", calculatePitch(a.x, a.y, a.z)]];
    [rollLabel setText: [NSString stringWithFormat:@"%.3fº", calculateRoll(a.x, a.y, a.z)]];
    [thrustLabel setText: [NSString stringWithFormat:@"%.3f", slider.value]];

	// Send the values to our peer
	if ((activeConnection != nil) && ([activeConnection isConnected])) {
		double *values = malloc(sizeof(double) * 4);
		// Note that we're swapping x with y since this will be held in landscape orientation
		values[0] = a.y;
		values[1] = a.x;
		values[2] = a.z;
		values[3] = (double)slider.value;
		
		[activeConnection sendData:[NSData dataWithBytesNoCopy:values length:4*sizeof(double) freeWhenDone:YES]];
	}
}

- (void)gamePeerDidConnect:(GamePeer *)peer
{
}

- (void)gamePeerDidDisconnect:(GamePeer *)peer
{
}

- (void)gamePeerDidFailConnecting:(GamePeer *)peer
{
}


-(void)gamePeerDidReceiveData:(NSData *)data
{
    if ([[NSString stringWithCString: [data bytes] encoding:NSUnicodeStringEncoding] isEqualToString:@"endOfJPEG"]){
        if (receivedImageData){
            UIImage * lastReceivedImage = [UIImage imageWithData: receivedImageData];
            [lastReceivedImageView setImage: lastReceivedImage];
            [receivedImageData release];
            receivedImageData = nil;
        }
        
    } else {
        if (!receivedImageData)
            receivedImageData = [[NSMutableData alloc] init];
        [receivedImageData appendData: data];
    }
}


@end
